/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


User = function(){
    
}

User.columns = new Array("Tên Đăng Nhập","Họ Và Tên","Email");
User.dataTables = new Array();
User.rows = 10;//tong so record duoc hien thi
User.numberPage = 1;//tong so trang
User.current_page = 1;//luu trang hien tai
User.sum_pages = 1;
//tao tieu de cua cot
User.titleColumns = function(_columns){
    var myColumns = "<table class=\"ui-responsive table-stroke\" style=\"width:100%;\">";
    myColumns += "<thead><tr><th class=\"\" scope=\"col\">STT</th>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"\" scope=\"col\">Edit & View</th>";
    myColumns += "<th class=\"\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 2;
    myColumns += "<tfoot><tr><td class=\"\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

User.dataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;//
    var r = rs;
    var count = 0;
    for (var i = j; count < r && i < n; i++) {
        count++;
        var id = data[i].id;        
        myDataColumns += "<tr id=\"" + id + "\">";
        myDataColumns += "<td>"+ count +"</td>";
        myDataColumns += "<td>" + data[i].username + "</td>"
        myDataColumns += "<td>" + data[i].fullname + "</td>"
        myDataColumns += "<td>" + data[i].email + "</td>"        
        myDataColumns += "<td><img onclick='return User.editItem(this)' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return User.deleteItem(this)' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";    
    return myDataColumns;
}

User.createTable = function(r, nbpage, data){
    $(".div_list").empty();//xoa tat ca cac html con trong the li co class la "div_list";
    if (data.length > 0) {
        var t = data.length / User.rows;
        var _pages = parseInt(data.length / User.rows) == t ? parseInt(t) : parseInt(t) + 1;
        User.sum_pages = _pages;//tong so trang
        var myTable = User.titleColumns(User.columns);
        myTable += User.dataColumnsOftr(data, r, nbpage);
        myTable += "</table>";
        $(".div_list").append(myTable);
        
        try{            
            $(".div_list").listview('refresh');
        }
        catch(e){
            
        }
    }    
}

User.editItem = function(obj){
    
}

User.deleteItem = function(obj){
    
}
User.load = function(){
    var url = Editor.Url + "UserController.php";
     $.ajax({
        type: "POST",
        url: url, // Location of the service								
        dataType: "json", //Expected data format from server
        cache: false,
        success: function(msg) {
           console.dir(msg);
           User.dataTables = msg;
           var t = User.dataTables.length / User.rows;
           User.createTable(User.rows, User.current_page, User.dataTables);
        },
        error: function(err) {
            // When Service call fails
        }
    });
}